<template>
  <div>
    <!-- 卡片 -->
    <el-card>
      <!-- row布局头部 -->
      <el-row class="topbread" v-if="subjectID">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subjectName}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <!-- 表单 -->
      <el-form  label-width="80px">
        <el-row>
          <el-col :span="4" >
            <el-form-item label="目录名称">
             <el-input v-model="searchOption.directoryName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4" >
            <el-form-item label="状态">
             <el-select v-model="statevalue" placeholder="请选择">
              <!-- 下拉框 -->
               <el-option
                 v-for="item in stateOptions"
                 :key="item.value"
                 :label="item.label"
                 :value="item.value">
               </el-option>
             </el-select>
            </el-form-item>
          </el-col>
          <!-- 按钮 -->
          <el-col :span="6" :offset="1">
            <el-button @click="clearOption">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-col>
          <!-- 新增 -->
          <el-col :span="9" class="titleBtn">
            <el-button type="text" class="back" icon="el-icon-back" v-if="subjectID" @click="$router.push('/subjects/list')">返回学科</el-button>
            <el-button type="success" icon="el-icon-edit" @click="handerEdit('add')">新增目录</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 记录 -->
      <el-alert
        type="info"
        :title="`共${searchRes.counts}条记录`"
        show-icon>
      </el-alert>
      <!-- 目录列表 -->
      <el-table
      :data="searchRes.items"
      style="width: 100%"
      :row-style="{height:'60px'}">
      <el-table-column
        prop="id"
        label="序号"
        width="90">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科"
        width="260">
      </el-table-column>
      <el-table-column
        prop="directoryName"
        label="目录名称"
        width="250">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者"
        width="230">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期"
        width="230">
      </el-table-column>
      <el-table-column
        prop="totals"
        label="面试题数量"
        width="230">
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        width="220">
        <template slot-scope="{row}">
          {{row.state===0?'禁用':'已启用'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="tags"
        label="操作">
        <template slot-scope="{row}">
          <el-button type="text" v-if="row.state===1" @click="changeState(row)">禁用</el-button>
          <el-button type="text" v-else @click="changeState(row)">启用</el-button>
          <el-button type="text" :disabled="row.state===1" @click="handerEdit('edit', row)">修改</el-button>
          <el-button type="text" :disabled="Boolean(row.totals||row.state===1)" @click="handleDelete(row)">删除</el-button>
      </template>
      </el-table-column>
      </el-table>
      <div class="bottomTool">
      <!-- 分页器 -->
      <PageTool :total='searchRes.counts' :paginationPage='searchOption.page' :paginationPagesize='searchOption.pagesize' @pageChange='pageChange' @pageSizeChange='pageSizeChange'>
      </PageTool>
    </div>
    </el-card>
    <!-- 弹框 -->
    <directorysAdd ref="subjectAdd" :isSingle='Boolean(subjectID)' :titleInfo='titleInfo' :formBase='formBase' @newDataes='newDataes'></directorysAdd>
  </div>
</template>

<script>
import moment from 'moment'
import directorysAdd from '@/module-hmmm/components/directorys-add'
import { list, remove, changeState } from '@/api/hmmm/directorys.js'/* 目录列表，目录删除，目录状态 */
import PageTool from '@/module-manage/components/page-tool'
export default {
  data () {
    return {
      // 搜索临时数据
      searchOption: {
        subjectName: '',
        pagesize: 10,
        page: 1
      },
      // 状态
      statevalue: '',
      // 状态信息
      stateOptions: [{ label: '启用', value: 1 }, { label: '禁用', value: 0 }],
      // 目录列表
      searchRes: [],
      // 弹框标题
      titleInfo: '',
      // 弹框临时数据
      formBase: {
        id: '',
        subjectID: '',
        directoryName: ''
      }
    }
  },
  computed: {
    subjectID () {
      return this.$route.query.id
    },
    subjectName () {
      return this.$route.query.name
    }
  },
  components: { PageTool, directorysAdd },
  // 一开始就获取数据
  async created () {
    this.getSearchOption()
  },
  methods: {
    //
    async getSearchOption () {
      const option = this.statevalue || this.statevalue === 0 ? { ...this.searchOption, state: this.statevalue } : this.searchOption
      if (this.subjectID) option.subjectID = this.subjectID
      const { data } = await list(option)
      data.items.forEach(item => { item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss') })/* 时间格式化 */
      this.searchRes = data
    },
    // 清除
    clearOption () {
      this.statevalue = ''
      this.searchOption = {
        subjectName: '',
        pagesize: 10,
        page: 1
      }
    },
    // 搜索
    search () {
      this.getSearchOption()
    },
    pageChange (id) {
      this.searchOption.page = id
      this.getSearchOption()
    },
    pageSizeChange (id) {
      this.searchOption.pagesize = id
      this.getSearchOption()
    },
    // 增加或修改目录
    handerEdit (type, id) {
      // 重置临时数据
      this.formBase = {
        id: '',
        subjectID: '',
        directoryName: ''
      }
      if (type === 'add') {
        // 新增
        this.titleInfo = '新增目录'
        if (this.subjectID) this.formBase.subjectID = +this.subjectID
        this.$refs.subjectAdd.dialogFormV()/* 弹层显示 */
      } else if (type === 'edit') {
        // 修改，数据回显
        this.titleInfo = '修改目录'
        this.formBase.id = id.id
        this.formBase.subjectID = id.subjectID
        this.formBase.directoryName = id.directoryName
        this.$refs.subjectAdd.dialogFormV()/* 弹层显示 */
      }
    },
    newDataes () {
      this.getSearchOption()
    },
    // 删除目录
    handleDelete (row) {
      // 警示
      this.$confirm('此操作将删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: row.id })
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.getSearchOption()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 禁用和启用状态切换
    async changeState (row) {
      await changeState({ id: row.id, state: row.state === 0 ? 1 : 0 })
      // 提示
      this.$message({
        type: 'success',
        message: '操作成功'
      })
      this.getSearchOption()
    }

  }
}
</script>

<style scoped lang='less'>
  .bottomTool{
    margin: 20px 0;
    float: right;
  }
.el-select{
  display: block;
}

.titleBtn{
  position: relative;
  .el-button{
    position: absolute;
    right: 0;
  }
  .back{
    position: absolute;
    right: 130px;
    margin-top: 7px;
    font-size: 16px;
  }
}
.btn{
  color: #66b1ff;
  margin-left: 8px;
  font-size: 16px;
}
</style>
